package org.yelongframework.servlet.http;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.yelongframework.commons.lang.IntegerUtil;

/**
 * 默认实现
 * 
 * @since 1.0.0
 */
public class DefaultHttpServletRequestSupport implements HttpServletRequestSupport {

	// ==================================================获取参数==================================================

	@Override
	public String getParameter(ServletRequest request, String name, String defaultValue) {
		String value = request.getParameter(name);
		return isNotEmpty(value) ? value : defaultValue;
	}

	@Override
	public Integer getParameterInteger(ServletRequest request, String name) {
		String value = request.getParameter(name);
		return isEmpty(value) ? null : IntegerUtil.valueOf(value);
	}

	@Override
	public Integer getParameterInteger(ServletRequest request, String name, Integer defaultValue) {
		Integer value = getParameterInteger(request, name);
		return null != value ? value : defaultValue;
	}

	@Override
	public Long getParameterLong(ServletRequest request, String name) {
		String value = request.getParameter(name);
		return isEmpty(value) ? null : Long.valueOf(value);
	}

	@Override
	public Long getParameterLong(ServletRequest request, String name, Long defaultValue) {
		Long value = getParameterLong(request, name);
		return null != value ? value : defaultValue;
	}

	@Override
	public Boolean getParameterBoolean(ServletRequest request, String name) {
		String value = request.getParameter(name);
		return isEmpty(value) ? null : Boolean.valueOf(value);
	}

	@Override
	public Boolean getParameterBoolean(ServletRequest request, String name, Boolean defaultValue) {
		Boolean value = getParameterBoolean(request, name);
		return null != value ? value : defaultValue;
	}

	@Override
	public String getParameterMapString(ServletRequest request) {
		Map<String, String[]> requestParams = request.getParameterMap();
		if (requestParams.isEmpty()) {
			return "";
		}
		StringBuilder requestParamStr = new StringBuilder();
		requestParams.forEach((key, value) -> {
			String valueStr = "";
			if (value.length == 1) {
				valueStr = value[0];
			} else {
				for (int i = 0; i < value.length; i++) {
					valueStr += (value[i] + ",");
				}
				valueStr = valueStr.substring(0, valueStr.length() - 1);
			}
			requestParamStr.append(key + "=" + valueStr + "&");
		});
		requestParamStr.deleteCharAt(requestParamStr.length() - 1);
		return requestParamStr.toString();
	}

	// ==================================================获取消息体==================================================

	@Override
	public byte[] readBodyByteArray(ServletRequest request) throws IOException {
		InputStream is = request.getInputStream();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		byte[] tmp = new byte[1024];
		int read;
		while ((read = is.read(tmp)) > 0) {
			os.write(tmp, 0, read);
		}
		return os.toByteArray();
	}

	@Override
	public String readBodyString(ServletRequest request) throws IOException {
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = request.getReader();
		char[] buff = new char[1024];
		int len;
		while ((len = reader.read(buff)) != -1) {
			sb.append(buff, 0, len);
		}
		return sb.toString();
	}

	// ==================================================工具==================================================

	private boolean isEmpty(String value) {
		return StringUtils.isEmpty(value);
	}

	private boolean isNotEmpty(String value) {
		return StringUtils.isNotEmpty(value);
	}

}
